package cn.com.self.apis.portal.controller;

import cn.com.self.apis.member.entity.UMemberInfo;
import cn.com.self.apis.member.service.IMemberInfoService;
import cn.com.self.apis.portal.entity.UDisputeEntity;
import cn.com.self.apis.portal.service.intf.IDisputeService;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import cn.com.selfsoft.smartcity.commons.http.DataPage;
import cn.com.selfsoft.smartcity.commons.http.ResultModel;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/dispute")
@Api(tags = "纠纷申请信息类")
public class DisputeController {
    @Autowired
    private IDisputeService disputeService;
    @Autowired
    private IMemberInfoService memberInfoService;
    /**新增纠纷申请
     * @Description //TODO
     * @return
     **/
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增纠纷申请")
    @Transactional
    public ResponseEntity create(@RequestBody UDisputeEntity entity, HttpServletRequest request){
        UMemberInfo uMemberInfo = memberInfoService.load(entity.getMemberId().toString());
        if(uMemberInfo == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ResultModel<>(404, "该发起人不存在！"));
        }
        UDisputeEntity create = disputeService.createDispute(entity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(create));
    }


    /**分页获取纠纷申请列表
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "分页获取纠纷申请列表")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity getEscrowInfoPage(HttpServletRequest request, Pageable page){
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");
        ICustomPage<UDisputeEntity> pageData = this.disputeService.page(queryParams, page);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(pageData));

    }

    /**获取纠纷申请信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "获取纠纷申请信息")
    @RequestMapping(value = {"/load/{id}"}, method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity loadEscrowInfo(@PathVariable("id") String id, HttpServletRequest request) {
        UDisputeEntity disputeEntity = this.disputeService.loadDisputeInfoById(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(disputeEntity)));

    }


    /**修改纠纷申请信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "修改纠纷申请信息")
    @RequestMapping(value = {"/update/{id}"}, method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody UDisputeEntity entity, HttpServletRequest request) {
        UDisputeEntity update = this.disputeService.updateDispute(id,entity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(update)));

    }


    /**删除纠纷申请信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "删除纠纷申请信息")
    @ResponseBody
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    @Transactional
    public ResponseEntity deletes(@PathVariable("id") String id){
        this.disputeService.delete(id);
        this.disputeService.deleteUFile(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }

}
